const app = getApp();
const { infoArr } = require("../../utils/cityInfo");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      avatarUrl: '../../images/tx.png',
      nickName: '请登录'
    },
    play: true,
    isShowAuth: !1,
    isShowAuth1: !1,
    myPhone: '',
    surplus: 0,
    zxLists: [],
    datasetNum: 0,
    isTeacher: false,
    workStudy: false,
    isskTeacher: false
  },
  phoneCall: function (e) {
    xhs.makePhoneCall({
      phoneNumber: '4006300999',
      success: function () {
        console.log(11); //打电话
      },
    })
  },
  onShareAppMessage: function () {
    return {
      title: '河北中公教育',
      path: 'pages/index/index',
      imageUrl: '',
      success: (res) => {
        // 分享成功
        console.log('success');
      },
      fail: (res) => {
        // 分享失败
        console.log('fail');
      }
    }
  },
  login: function () {
    let that = this;
    xhs.getStorage({
      key: 'userInfo',
      success(res) {
        if (res.data.nickName != '微信用户') {
          that.setData({
            userInfo: res.data
          })
        } else {
          that.setData({
            isShowAuth1: !0
          })
        }
      },
      fail(res) {
        that.setData({
          isShowAuth1: !0
        })
      }
    })
    xhs.getStorage({
      key: 'myPhone',
      success(res) {
        myPhone: res.data
      },
      fail(res) {
        that.setData({
          isShowAuth: !0
        })
      }
    })
  },
  user_login: function () {
    console.log(1)
    this.setData({
      userInfo: xhs.getStorageSync("userInfo")
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.watch(this.watchmyPhone)
    xhs.showShareMenu({
      shareTypes: 'normal',
    })
    this.setData({
      options
    })
    console.log(app.globalData.kf)
  },
  watchmyPhone: function (value) {
    let that = this;
    if (value.userInfo) {
      if (value.userInfo.nickName != '微信用户') {
        that.setData({
          userInfo: value.userInfo,
        })
      }
    }
  },
  isskTeacher: function (myPhone) {
    console.log(myPhone)
    xhs.request({
      url: "https://hbapp.offcn.com/hbqt/web/index.php/Contactus/xcxhdtwo/teacherfk_shizi_info?callback=&",
      type: 'GET',
      dataType: 'jsonp',
      data: {
        phone: myPhone,
      },
      success: (res => {
        var resdata = JSON.parse(res.data.substr(1, res.data.length - 2));
        console.log(resdata)
        if (resdata.status == 1) {
          this.setData({
            isskTeacher: true
          })
        }
      })
    })
  },
  // 登录授课反馈系统
  toskTeacher: function () {
    xhs.navigateTo({
      url: '/packageB/feedback/feedback',
    })
  },
  // 判断是否登陆过
  toTeacher: function () {
    let that = this;
    xhs.getStorage({
      key: 'teacherLogin',
      success: function (res) {
        if (res.data) {
          xhs.cloud.callFunction({
            name: 'zixun',
            data: {
              "form": 'teacher',
              "myPhone": that.data.myPhone
            },
            success: function (res) {
              if (res.result.data.userInfo.getstaus) {
                console.log(1)
                xhs.navigateTo({
                  url: '/packageA/teacher/list/list?phone=' + that.data.myPhone,
                })
              } else {
                console.log(2)
                that.tip('提示', '您的师资身份已被关闭，有问题请于管理员联系！')
              }
            },
            fail: function (err) {
              console.log(3)
              that.tip('提示', '您已不在师资库中，有问题请于管理员联系！')
            }
          })
        } else {
          console.log(4)
          xhs.navigateTo({
            url: '/packageA/teacher/login/login',
          })
        }
      },
      fail: function (err) {
        if (app.globalData.teacherLogin) {
          xhs.navigateTo({
            url: '/packageA/teacher/list/list?phone=' + app.globalData.teacherPhone,
          })
        } else {
          xhs.navigateTo({
            url: '/packageA/teacher/login/login',
          })
        }
      }
    })
  },
  async zixun() {
    let { kf } = app.globalData.info;
    console.log(kf)
    if(!kf) {
        let {
            kf,
            dyhewm,
            dyh,
            phone
        } = await this.getInfo();
        this.setData({
            kf,
            dyhewm,
            dyh,
            phone
        })
        app.globalData.info = {
            kf,
            dyhewm,
            dyh,
            phone
        }
        app.methods.goZixun(kf)
    } else {
      app.methods.goZixun(kf)
    }
  },
  getInfo() {
    this.setData({
      authSetting: app.authSetting
    })
    return new Promise(async resolve => {
      if (this.data.options.fenxiao) {
        let info = await app.methods.getCity(infoArr, this.data.options.fenxiao)
        resolve(info)
      } else {
        let info = await app.methods.noFenxiao(infoArr)
        resolve(info)
      }
    })
  },
  workStudy: function () {
    let that = this;
    xhs.request({
      url: 'https://hbapp.offcn.com/hbqt/web/index.php/Contactus/qgzxjh/getjianzhis_index?callback=&',
      type: 'GET',
      dataType: 'jsonp',
      data: {
        phone: that.data.myPhone,
      },
      success(res) {
        var resdata = JSON.parse(res.data.substr(1, res.data.length - 2));
        console.log(resdata)
        that.setData({
          workStudy: true
        })
      },
      fail(err) {
        console.log(err)
      }
    })
  },
  toWorkStudy: function () {
    xhs.navigateTo({
      url: '/packageA/workStudy/workStudy',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // let that = this;
    // xhs.getStorage({
    //   key: 'myPhone',
    //   success: function (res) {
    //     xhs.cloud.callFunction({
    //       name: 'zixun',
    //       data: {
    //         "form": "zixun",
    //         "myPhone": res.data
    //       },
    //       success(res) {
    //         that.setData({
    //           surplus: res.result.data.total - res.result.data.zixun.length,
    //           zxLists: res.result.data.zixun
    //         })
    //       },
    //       fail(err) {
    //         console.log(err)
    //       }
    //     })
    //   },
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})